package jsu.hx.lost.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jsu.hx.lost.Model.dto.LostItemDTO;
import jsu.hx.lost.Model.entity.FindItem;
import jsu.hx.lost.Model.dto.FindItemDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface FindMapper extends BaseMapper<FindItem> {
    // FindMapper.java 新增方法
    @Select("SELECT fi.id, fi.addtime, fi.item_name, fi.image, fi.item_category, " +
            "fi.found_location, fi.found_time,fi.locate, fi.detailed_description, " +
            "up.username, up.full_name, up.phone " +
            "FROM find_items fi " +
            "JOIN user_profiles up ON fi.user_id = up.id " +
            "WHERE fi.user_id = #{userId} AND fi.is_deleted = 0 AND up.is_deleted = 0")
    List<FindItemDTO> selectFindItemDTOsByUserId(@Param("userId") Long userId);
    List<FindItemDTO> selectFindItemDTOs();
    // FindMapper.java
    List<FindItemDTO> selectByFuzzySearch(
            @Param("itemName") String itemName,
            @Param("foundLocation") String foundLocation,
            @Param("foundTime") Date foundTime,
            @Param("username") String username // 新增参数
            );
    @Select("SELECT fi.id, fi.addtime, fi.item_name, fi.image, fi.item_category, fi.found_location, fi.locate,fi.found_time, fi.detailed_description, up.username, up.full_name, up.phone " +
            "FROM find_items fi " +
            "JOIN user_profiles up ON fi.user_id = up.id " +
            "WHERE fi.id = #{id} AND fi.is_deleted = 0 AND up.is_deleted = 0")
    FindItemDTO selectFindItemDTOById(@Param("id") Long id);

//    @Select("SELECT fi.id, fi.addtime, fi.item_name, fi.image, fi.item_category, " +
//            "fi.found_location, fi.found_time, fi.detailed_description, " +
//            "up.username, up.full_name, up.phone " +
//            "FROM find_items fi " +
//            "JOIN user_profiles up ON fi.user_id = up.id " +
//            "WHERE fi.is_deleted = 0 AND up.is_deleted = 0 " +
//            "AND (#{itemName} IS NULL OR fi.item_name LIKE CONCAT('%', #{itemName}, '%')) " +
//            "AND (#{foundLocation} IS NULL OR fi.found_location LIKE CONCAT('%', #{foundLocation}, '%')) " +
//            "AND (#{foundTime} IS NULL OR fi.found_time = #{foundTime}) " +"ORDER BY ${sortField} ${sortOrder} " +
//            "LIMIT #{offset}, #{limit}")
    List<FindItemDTO> selectFindItemDTOsByPage(
            @Param("offset") int offset,
            @Param("pageSize") int pageSize,
            @Param("itemName") String itemName,
            @Param("foundLocation") String foundLocation,
            @Param("foundTime") Date foundTime,
            @Param("username") String username, // 新增参数
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder);
    // FindMapper.java
//    @Select("SELECT COUNT(*) FROM find_items fi " +
//            "JOIN user_profiles up ON fi.user_id = up.id " +
//            "WHERE fi.is_deleted = 0 AND up.is_deleted = 0")
    long selectFindItemTotalCount(
            @Param("itemName") String itemName,
            @Param("foundLocation") String foundLocation,
            @Param("foundTime") Date foundTime,
            @Param("username") String username);
    @Select("SELECT fi.id, fi.addtime, fi.item_name, fi.image, fi.item_category, " +
            "fi.found_location, fi.found_time, fi.detailed_description, " +
            "up.username, up.full_name, up.phone " +
            "FROM find_items fi " +
            "JOIN user_profiles up ON fi.user_id = up.id " +
            "WHERE fi.is_deleted = 0 AND up.is_deleted = 0 " +"ORDER BY addtime DESC LIMIT #{limit}")
    List<FindItemDTO> selectLatestFoundItems(@Param("limit") int limit);
}
